<?php
Class MedicalrecordAction extends Action {
	public function user(){//管理用户病历首页
		$user['id'] = $medrecord['user_id']=$this->userid = intval(I('id'));
		$this->user = M('User')->where($user)->find();//当前用户的一些基本信息

		$medrecord['pubtype'] = $this->pubtype = intval(I('pubtype',2));//1草稿2发布3屏蔽（不做物理删除）
		//导入分页插件/该层是在editurl中打开的
		import('ORG.Util.EditPage');
		$count = M('MedicalRecords')->where($medrecord)->count();
		$Page = new Page($count,10);
        $Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		$this->page = $Page->show();
        $this->list = M('MedicalRecords')->where($medrecord)->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo M('MedicalRecords')->getLastSql();
        $this->currentpage = $Page->firstRow + 1; 
		$this->display();
	}

	public function report(){//病例分享单个病例报告1
		$user['id'] = $medrecord['user_id']=$this->userid = intval(I('id'));
		$this->user = $userinfo = M('User')->where($user)->find();//当前用户的一些基本信息
		if(I('stime') && I('etime')){
			$condition['pubtime']=array('between',strtotime(I('stime')),strtotime(I('etime')));
			$condition['pubtype']=2;
			$condition['user_id']=I('id');
		}
		$price = 0;
		$list = M('MedicalRecords')->where($condition)->select();
		foreach ($list as $key => $value) {
			$expert = M('MedicalRecordsDiscuss')->where("rec_id = {$value['id']} and order_num <> ''")->select();
			foreach ($expert as $k => $v) {
				$discussprice = M('UserLevel')->where("id = {$userinfo['level']}")->getField('discussprice');
				$price = $price + $discussprice;
			}
			$list[$key]['price']=$price;//点评价格总和
			$list[$key]['expert'] = $expert;//特约点评专家
		}

	}

	public function edit(){
		if(IS_POST){
			$condition['id']=intval(I('id'));
			$data['cashtype']=intval(I('cashtype'));
			$data['gender']=I('gender');
			$data['age']=intval(I('age'));
			$data['major']=I('major');
			$data['complain']=I('complain');
			$data['history']=I('history');
			$data['oralhistory']=I('oralhistory');
			$data['profile']=I('profile');
			$data['normalcheck']=I('normalcheck');
			$data['specialcheck']=I('specialcheck');
			$data['diagnostic']=I('diagnostic');
			$data['tp']=I('tp');
			$data['rediagnosis']=I('rediagnosis');
			$data['prognosis']=I('prognosis');
			$data['discuss']=I('discuss');
			$searchtext = implode(" ",$data);
			$data['searchtext']=$searchtext;
			if($image = I('images')){
				$data['image'] = "./Public/static/is_pic.jpg";
				M('MedicalRecordsImages')->where("rec_id={$condition['id']}")->delete();
				$images = explode(",",$image);
				for($i=0;$i<count($images);$i++){
					$medrecord['image']=$images[$i];
					$medrecord['rec_id']=$condition['id'];
					M('MedicalRecordsImages')->add($medrecord);
					//$this->ajaxReturn(array('status'=>0,"msg"=>print_r($images) ),"JSON");exit;
				}
			}else{
				$data['image'] = "./Public/static/no_pic.jpg";
			}
			$data['savetime']=time();
			$save = M('MedicalRecords')->where($condition)->save($data);
			if($save!==false){
				$this->ajaxReturn(array('status'=>1),"JSON");exit;
			}else{
				$this->ajaxReturn(array('status'=>0),"JSON");exit;
			}
		}
		$merecord['id']=intval(I('id'));
		$user['id']=$this->userid=intval(I('userid'));
		$this->merecord = M("MedicalRecords")->where($merecord)->find();
		//print_r($this->merecord);
		$this->user = M('User')->where($user)->find();
		$this->age = M('MedicalRecordsAge')->select();
		$this->images =M('MedicalRecordsImages')->where("rec_id={$merecord['id']}")->select();
		$this->major = M('Major')->select();
		$this->pubtype=intval(I("pubtype"));
		$this->page=intval(I('p'));
		$this->display();
	}

	public function add(){
		if(IS_POST){
			$data['user_id']=intval(I('userid'));
			$data['cashtype']=intval(I('cashtype'));
			$data['gender']=I('gender');
			$data['age']=intval(I('age'));
			$data['major']=I('major');
			$data['complain']=I('complain');
			$data['history']=I('history');
			$data['oralhistory']=I('oralhistory');
			$data['profile']=I('profile');
			$data['normalcheck']=I('normalcheck');
			$data['specialcheck']=I('specialcheck');
			$data['diagnostic']=I('diagnostic');
			$data['tp']=I('tp');
			$data['rediagnosis']=I('rediagnosis');
			$data['prognosis']=I('prognosis');
			$data['discuss']=I('discuss');
			$searchtext = implode(" ",$data);
			$data['searchtext']=$searchtext;
			$data['savetime']=$data['addtime']=time();	
			$add = M('MedicalRecords')->add($data);
			if($add){
				if($image = I('images')){
					$data['image'] = "./Public/static/is_pic.jpg";
					$images = explode(",",$image);
					for($i=0;$i<count($images);$i++){
						$medrecord['image']=$images[$i];
						$medrecord['rec_id']=$add;
						M('MedicalRecordsImages')->add($medrecord);
					}
				}else{
					$data['image'] = "./Public/static/no_pic.jpg";
				}
				$this->ajaxReturn(array('status'=>1),"JSON");exit;
			}else{
				$this->ajaxReturn(array('status'=>0),"JSON");exit;
			}		
		}
		$user['id']=$this->userid=intval(I('userid'));
		$this->user = M('User')->where($user)->find();
		$this->age = M('MedicalRecordsAge')->select();
		$this->major = M('Major')->select();
		$this->pubtype=intval(I("pubtype"));
		$this->page=intval(I('p'));
		$this->display();
	}

	public function publish(){
		$condition['id']=intval(I('id'));
		$data['pubtype']=2;
		$data['pubtime']=time();
		$data['ispass']=2;//发布进入审核状态
		$save = M('MedicalRecords')->where($condition)->save($data);
		if($save!==false){
			$this->ajaxReturn(array("status"=>1),"JSON");
		}else{
			$this->ajaxReturn(array("status"=>0),"JSON");
		}
	}

	public function savetemp(){
		$condition['id']=intval(I('id'));
		$data['pubtype']=1;
		$data['savetime']=time();
		$save = M('MedicalRecords')->where($condition)->save($data);
		if($save!==false){
			$this->ajaxReturn(array("status"=>1),"JSON");
		}else{
			$this->ajaxReturn(array("status"=>0),"JSON");
		}
	}

	public function importrecords(){
		if($filename = $this->uploadcsv()){
			$handle = fopen($filename, 'r'); 
			$result = $this->input_csv($handle); //解析csv 
			$len_result = count($result); 
			if($len_result==0){ 
			    $this->ajaxReturn(array('status'=>0,'msg'=>'没有任何数据'),"JSON");exit;
			}
		    for ($i = 1; $i < $len_result; $i++) {
		        $data['gender'] = iconv('gb2312', 'utf-8', $result[$i][0]); //中文转码 
		        $data['age'] = $result[$i][1]; 
		        $data['complain'] = iconv('gb2312', 'utf-8', $result[$i][2]); 
		        $data['history'] =  iconv('gb2312', 'utf-8', $result[$i][3]);
		        $data['oralhistory'] = iconv('gb2312', 'utf-8', $result[$i][4]);
		        $data['profile'] = iconv('gb2312', 'utf-8', $result[$i][5]);
		        $data['normalcheck'] = iconv('gb2312', 'utf-8', $result[$i][6]);
		        $data['specialcheck'] = iconv('gb2312', 'utf-8', $result[$i][7]);
		        $data['diagnostic'] = iconv('gb2312', 'utf-8', $result[$i][8]);
		        $data['major'] = iconv('gb2312', 'utf-8', $result[$i][9]);
		        $data['tp'] = iconv('gb2312', 'utf-8', $result[$i][10]);
		        $data['rediagnosis'] = iconv('gb2312', 'utf-8', $result[$i][11]);
		        $data['prognosis'] = iconv('gb2312', 'utf-8', $result[$i][12]);
		        $data['discuss'] = iconv('gb2312', 'utf-8', $result[$i][13]);
		        $data['image'] = "./Public/static/no_pic.jpg";//默认没有图片
		        $data['user_id']=intval(I('userid'));
		        $searchtext = implode(" ", $data);
		        //$data['level']=$result[$i][14]; 
		        $data['searchtext']=$searchtext;
		        $data['addtime']=time();//创建时间
		        $data['savetime']=time();//存草稿时间
		        //$data['cashtype']=$result[$i][15]; 
		        $data['ispass']=2;//后台没有审阅过
		        if($isUser = M('User')->where("id={$data['user_id']}")->getField('id')){
			        $add = M('MedicalRecords')->add($data);
			        if(!$add){
			        	fclose($handle);
			        	$this->ajaxReturn(array('status'=>0,'msg'=>M('MedicalRecords')->getLastSql()),"JSON");exit;
			        	
			        }else{
					    fclose($handle); //关闭指针 
					    $this->ajaxReturn(array('status'=>1),"JSON");exit;	
					   		        	
			        }		        	
		        }else{
			        	fclose($handle);
			        	$this->ajaxReturn(array('status'=>0,'msg'=>'用户不存在'),"JSON"); exit;	
		        }
		    } 

		}else{
			$this->ajaxReturn(array('status'=>0,'msg'=>$filename),"JSON");exit;
		}
	}

	private function uploadcsv(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		//$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('csv');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/csv/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			return 0;
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			return $upload->savePath.$info[0]['savename'];
		}		
	}

	private function input_csv($handle){
		setlocale(LC_ALL,array('zh_CN.gbk','zh_CN.gb2312','en_US.utf8'));
	    $out = array (); 
	    $n = 0; 
	    while ($data = fgetcsv($handle, 10000)) { 
	        $num = count($data); 
	        for ($i = 0; $i < $num; $i++) { 
	            $out[$n][$i] = $data[$i]; 
	        } 
	        $n++; 
	    } 
	    return $out; 
	}

	public function getrecord(){
		$condition['id']=I('id');
		$data['list'] = M('MedicalRecords')->where($condition)->find();
		$data['list']['age']=M('MedicalRecordsAge')->where("id={$data['list']['age']}")->getField('age');
		$data['image'] = M('MedicalRecordsImages')->where("rec_id={$condition['id']}")->select();
		$this->ajaxReturn($data,"JSON");
	}
}